package com.win.cn.core.pane;

import com.win.cn.WinStart;
import com.win.cn.control.SoftWareControl;
import com.win.cn.core.model.NameControl;

import javafx.event.EventHandler;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;

/**
 * 中间资源管理器模板
 * 
 * 
 * 博客教程 ：http://www.wingmei.cn/2017/05/06/从零开始学习javafx19-控件篇之treeview/
 * 官方教程：https://docs.oracle.com/javafx/2/ui_controls/tree-view.htm
 * @author wangzh
 * 2018年6月6日 上午11:11:42
 */
public class ResourcesPane {
	
	public AnchorPane createResourcesPane(){
		AnchorPane resourcesPane=new AnchorPane();
		resourcesPane.setMaxWidth(180.0);
		resourcesPane.setMinWidth(180.0);
		
		resourcesPane.getChildren().add(getToolTree());
		
    	
		return resourcesPane;
	}
	
	/**
	 * 开启软件
	 */
	public TreeView<String> getStartTree(){
		TreeView<String> treeView=new TreeView<>();
		treeView.setPrefHeight(1000.0);
		treeView.setPrefWidth(180);
		
		TreeItem<String> softWareTool = new TreeItem<>(NameControl.software);
		treeView.setRoot(softWareTool);
		softWareTool.setExpanded(true);
		
		TreeItem<String> timeTool = new TreeItem<>(NameControl.startPartion);
		softWareTool.getChildren().add(timeTool);
		
		treeView.setOnMouseClicked(new EventHandler<MouseEvent>(){
		    @Override
		    public void handle(MouseEvent mouseEvent){
		    	
		        //单击
		        if(mouseEvent.getClickCount() == 1){
		            TreeItem<String> onclick = treeView.getSelectionModel().getSelectedItem();
		            onSoftWareClick(onclick);

		        }
		    }
		});
		
		return treeView;
	}
	
	/**
	 * 工具菜单
	 */
	@SuppressWarnings("unchecked")
	public TreeView<String> getToolTree(){
		TreeView<String> treeView=new TreeView<>();
		
		treeView.setPrefHeight(1000.0);
		treeView.setPrefWidth(180);
		
		TreeItem<String> tool = new TreeItem<>(NameControl.tool);
		treeView.setRoot(tool);
		//设置树的展开状态
		tool.setExpanded(true);
		TreeItem<String> timeTool = new TreeItem<>(NameControl.time);
		
		treeView.setOnMouseClicked(new EventHandler<MouseEvent>(){
		    @Override
		    public void handle(MouseEvent mouseEvent){
		    	//双击
		        if(mouseEvent.getClickCount() == 2){

		        }
		        //单击
		        if(mouseEvent.getClickCount() == 1){
		            TreeItem<String> onclick = treeView.getSelectionModel().getSelectedItem();
		            onToolClick(onclick);

		        }
		    }
		});
		
		TreeItem<String> weather = new TreeItem<>(NameControl.weather);
		TreeItem<String> web = new TreeItem<>(NameControl.web);
		TreeItem<String> space=new TreeItem<>(NameControl.space);
		tool.getChildren().addAll(space,weather,web,timeTool);
		
		return treeView;
	}
	
	/**
	 * 软件栏
	 * @param item
	 */
	public void onSoftWareClick(TreeItem<String> item){
		if(item!=null){
			String name=item.getValue();
			if(name.equals(NameControl.startPartion)||name.equals(NameControl.software)){
				WinStart.borderPane.setCenter(new SoftWareControl().createTableView());
			}else{
				WinStart.borderPane.setCenter(null);
			}
		}
	}
	
	/**
	 * 工具栏
	 */
	public void onToolClick(TreeItem<String> item ){
		if(item!=null){
			String name=item.getValue();
			if(name.equals(NameControl.time)||name.equals(NameControl.tool)){
				WinStart.borderPane.setCenter(new DetailPane().createTimePane());
			}else if(name.equals(NameControl.weather)){
				WinStart.borderPane.setCenter(new DetailPane().createWeatherPane());
			}else if(name.equals(NameControl.web)){
				WinStart.borderPane.setCenter(new DetailPane().createWebRequest());
			}else if(name.equals(NameControl.space)){
				WinStart.borderPane.setCenter(new DetailPane().createSpace());
			}else{
				WinStart.borderPane.setCenter(null);
			}
		}
	}

}
